#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
//#include<string>
#include<string.h>
#include<stdlib.h>
using namespace std;
namespace str
{

	class string
	{
		//friend ostream& operator<<(ostream& out, string& d1);

	public:
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		string(const char* str = "")
			: _size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		char& operator[](size_t d1)
		{
			return _str[d1];
		}
		const char& operator[](size_t d1) const
		{
			return _str[d1];
		}
		void reserved(size_t d1)
		{
			char* cut = new char[d1 + 1];
			//memcpy(cut, _str, _size);
			strcpy(cut, _str);
			delete[] _str;
			_str = cut;
			_capacity = d1;
		}
		size_t size()
		{
			return _size;
		}
		const size_t size() const
		{
			return _size;
		}
		size_t capacity()
		{
			return _capacity;
		}
		void push_back(char d1)
		{
			if (_size == _capacity)
			{
				reserved(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size] = d1;
			_size++;
		}
		/*char c_str(int i)
		{
			return _str[i];
		}*/
		string& operator+=(char d1)
		{
			push_back(d1);
		}
		bool operator<(string& d1)
		{
			size_t i = 0;
			for (; i < _size; i++)
			{
				if (size() > i && _str[i] > d1._str[i])
				{
					return false;
				}
				if (size() == i == d1._size)
				{
					return false;
				}
			}
			return true;
		}
		void insert(size_t  i  ,const char* d1)
		{
			assert(i < _size);

			int len = strlen(d1);
			if (_size + len >= _capacity)
			{
				reserved(_capacity * 2);
			}
			int end = (int)_size;
			int pos = (int)i;
			while (end+len > pos)
			{
				_str[end + len] = _str[end];
				end--;
				if (end == 0)
				{
					break;
				}
			}
			if(i == 0)
				_str[len] = _str[end];
			else
				_str[len+1] = _str[end];

				_size = _size + len;
	
			int j = 0;
			while (len)
			{
				_str[pos] = d1[j];
				j++;
				len--;
				pos++;
			}
		}
		bool operator==(string& d1)
		{
			size_t i = 0;
			if (_size != d1.size())
			{
				return false;
			}
			for (; i < _size; i++)
			{
				if (_str[i] != d1._str[i])
				{
					return false;
				}
			}
			return true;
		}
		//void test1()
		//{
		//	cout << _str << endl;
		//}
		//private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
	ostream& operator<<(ostream& out, const string& d1)
	{
		for (size_t i = 0; i < d1.size(); i++)
		{
			out << d1[i];
		}
		/*for (auto a : d1)
			out << a;*/
		return out;
	}
}


void test1()
{
	str::string d1("hello world");
	cout << d1 << endl;
	d1.insert(9, "xxxx");
	cout << d1 << endl;
}
int main()
{
	test1();
	return 0;
}